package com.taiji.dicom4cloud.smart.alarm.socket;

import com.taiji.dicom4cloud.common.core.utils.CommonUtils;
import com.taiji.dicom4cloud.smart.alarm.utils.MessageUtils;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import java.util.Arrays;
import java.util.Base64;

/**
 * @Description
 * @Author fanxl
 * @Date 2021/8/16
 **/
public class SocketCliet {
    Socket socket = null;

    DataInputStream inStream = null;

    DataOutputStream outStream = null;

    public static void main(String[] args) {
//        SocketCliet socketCliet = new SocketCliet();

        //Original byte[]
        byte[] bytes = "@@\u0006AM21030002ÿ\u0001\u0004a~¢ Ð¿##".getBytes();

        //Base64 Encoded
        String encoded = Base64.getEncoder().encodeToString(bytes);

        //Base64 Decoded
        byte[] decoded = Base64.getDecoder().decode(encoded);

        byte[] data = MessageUtils.getData(decoded);

        short test = CommonUtils.bytesToShort(Arrays.copyOfRange(data, 1, 2));
        //Verify original content
        System.out.println( new String(decoded) );
    }

    public SocketCliet() {
        try {
            init();
            waitData();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    void init() throws Exception {
        socket = new Socket("47.99.37.153", 8899);
        inStream = new DataInputStream(socket.getInputStream());
        outStream = new DataOutputStream(socket.getOutputStream());
        socket.setSoTimeout(30000);
    }

    void waitData() {
        boolean flag = true;
        while (flag) {
            try {
                byte[] sendStr =  new byte[1000];
                sendStr[0] = 0x40;
                sendStr[1] = 0x40;
                sendStr[2] = 0x04;
                sendStr[3] = 0x14;
                sendStr[4] = 0x24;

                sendStr[5] = 0x31;
                sendStr[6] = 0x31;
                sendStr[7] = 0x31;
                sendStr[8] = 0x31;
                sendStr[9] = 0x31;
                sendStr[10] = 0x31;
                sendStr[11] = 0x31;
                sendStr[12] = 0x31;
                sendStr[13] = 0x31;
                sendStr[14] = 0x32;

                sendStr[15] = 0x14;
                sendStr[16] = 0x14;

                sendStr[17] = 0x24;
                //消息id
                sendStr[18] = 0x00;
                sendStr[19] = 0x04;

                sendStr[20] = 0x00;
                sendStr[21] = 0x01;

                sendStr[22] = 0x01;

                sendStr[23] = 0x02;
                sendStr[24] = 0x02;

                sendStr[25] = 0x23;
                sendStr[26] = 0x23;

                outStream.write(sendStr);

            } catch (Exception e) {
                e.printStackTrace();
                break;
            }
        }
    }
}

